package cn.hnu.qq.global;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JOptionPane;
import javax.swing.JProgressBar;

public class FileSender extends Thread
{
    private Socket socket = null;//�ͻ���socket
    private OutputStream out = null;//�������д���
    private int RECEIVER_PORT = -1;//�����߶˿�
    
    private File fileToSend = null;//Ҫ������ļ�
	private String RECEIVER_IP = "";
	
	private JProgressBar progress;
	private boolean sendOver = false;
    
    public FileSender(File file,String Receiver_IP,int Receiver_Port)
    {
        this.fileToSend = file;
        this.RECEIVER_PORT = Receiver_Port;//���ý��շ��˿�
        this.RECEIVER_IP = Receiver_IP;
        try 
        {
            this.socket = new Socket(RECEIVER_IP ,RECEIVER_PORT);
            out = socket.getOutputStream();//������߷���ݵ�
        } 
        catch (UnknownHostException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void run()
    {
        byte[] cmd = new byte[128];
        byte[] tcmd = "cp".getBytes();
        for(int i=0;i<tcmd.length;i++)
        {
            cmd[i] = tcmd[i];   
        }   
        cmd[tcmd.length] = -0;   
        try 
        {
            out.write(cmd,0,cmd.length);
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }   
         //�ļ���   
        byte[] file = new byte[256];   
        byte[] tfile = this.fileToSend.getName().getBytes();   
        for(int i=0;i<tfile.length;i++)
        {
            file[i] = tfile[i];   
        }   
        file[tfile.length] = -0;   
        try 
        {
            out.write(file,0,file.length);
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }          
         //��С      
        byte[] size = new byte[64];   
        byte[] tsize = (""+this.fileToSend.length()).getBytes();   
        int total = (int) this.fileToSend.length();
        progress.setMaximum(total);
        progress.setMinimum(0);
        for(int i=0;i<tsize.length;i++)
        { 
            size[i] = tsize[i];   
        }   
           
        size[tsize.length] = -0;   
        try 
        {
            out.write(size,0,size.length);
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
           
        FileInputStream fis = null;   
        byte[] buf = new byte[1024*10];    
        try 
        {
            fis = new FileInputStream(this.fileToSend);
        } 
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        }   
        int readsize = 0;
        int send = 0;
         try 
         {
            while((readsize = fis.read(buf, 0, buf.length))>0)
            {   
                out.write(buf,0,readsize);   
                out.flush();  
                send += readsize;
                progress.setValue(send);
            }
            
            sendOver = true;
        } 
        catch (IOException e)
        {
        	JOptionPane.showMessageDialog(null, "文件发送失败");
            e.printStackTrace();
        }   

    }
    
    public boolean isSendOver(){
    	return sendOver;
    }

	public JProgressBar getProgress() {
		return progress;
	}

	public void setProgress(JProgressBar progress) {
		this.progress = progress;
	}
    
}


